package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.comon.ResultInfo;
import com.itheima.reggie.domain.Setmeal;
import com.itheima.reggie.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/setmeal")
public class SetmealController {
    @Autowired
    private SetmealService setmealService;

    //分页查询
    @GetMapping("page")
    public ResultInfo<Page<Setmeal>> findPage(@RequestParam(defaultValue = "1") Integer page,
                                              @RequestParam(defaultValue = "10") Integer pageSize, String name) {

        //调用service层进行分页查询;
        Page<Setmeal> setmealPage = setmealService.findPage(page, pageSize, name);
        return ResultInfo.success(setmealPage);
    }


    //添加套餐
    @PostMapping
    @CacheEvict(value = "setmeal" ,allEntries = true)
    public ResultInfo<Object> saveSetmeal(@RequestBody Setmeal setmeal) {
        //调用Service进行处理
        setmealService.saveSetmeal(setmeal);
        return ResultInfo.success(null);
    }

    //根据id查询
    @GetMapping("/{id}")
    public ResultInfo<Setmeal> findById(@PathVariable("id") Long id) {
        //调用Service层处理
        Setmeal setmeal = setmealService.findById(id);
        return ResultInfo.success(setmeal);
    }

    //修改
    @PutMapping
    @CacheEvict(value = {"setmeal","dishInSetmeal"} ,allEntries = true)
    public ResultInfo<Object> updateById(@RequestBody Setmeal setmeal) {
        //调用Service处理
        setmealService.updateSetmeal(setmeal);
        return ResultInfo.success(null);
    }

    //停售与启售
    @PostMapping("/status/{value}")
    @CacheEvict(value = {"setmeal","dishInSetmeal"} ,allEntries = true)
    public ResultInfo<Object> updateStatus(@PathVariable("value") Integer status, @RequestParam("ids") List<Long> ids) {
        //创建对象
        Setmeal setmeal = new Setmeal();
        //赋值
        setmeal.setStatus(status);
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(Setmeal::getId, ids);
        setmealService.update(setmeal, wrapper);
        return ResultInfo.success(null);
    }

    //批量删除
    @DeleteMapping
    @CacheEvict(value = {"setmeal","dishInSetmeal"} ,allEntries = true)
    public ResultInfo<Object> deleteSetmeal(@RequestParam("ids") List<Long> ids) {
        setmealService.deleteSetmeal(ids);
        return ResultInfo.success(null);
    }
}
